package com.resou.reader.utils;

import com.resou.reader.reader.m.BreakResult;
import com.resou.reader.reader.m.ShowChar;

import java.util.ArrayList;

public class ToJavaTestUtil {
    public static BreakResult testToJava(char[] cs, float measureWidth, float textPadding) {
        if (cs == null || cs.length == 0) {
            return null;
        }

        BreakResult breakResult = new BreakResult();
        breakResult.showChars = new ArrayList<>();
        float width = 0;

        for (int i = 0, size = cs.length; i < size; i++) {
            String measureStr = String.valueOf(cs[i]);
            float charWidth = 30;

            //测出校验是否是<br><br> 是则换行
            if (cs.length - i >= ReaderUtil.WRAP_MARK.length()) {
                if ("<".equals(String.valueOf(cs[i])) && "b".equals(String.valueOf(cs[i + 1]))
                        && "r".equals(String.valueOf(cs[i + 2])) && ">".equals(String.valueOf(cs[i + 3]))
                        && "<".equals(String.valueOf(cs[i + 4])) && "b".equals(String.valueOf(cs[i + 5]))
                        && "r".equals(String.valueOf(cs[i + 6])) && ">".equals(String.valueOf(cs[i + 7]))) {
                    breakResult.chartNums = i + 8;
                    breakResult.isFullLine = true;
                    breakResult.endWithWrapMark = true;

                    return breakResult;
                }
            }

            if (width <= measureWidth && (width + textPadding + charWidth) > measureWidth) {
                breakResult.chartNums = i;
                breakResult.isFullLine = true;
                return breakResult;
            }

            ShowChar showChar = new ShowChar();
            showChar.charData = cs[i];
            showChar.charWidth = charWidth;
            breakResult.showChars.add(showChar);
            width += charWidth + textPadding;
        }

        breakResult.chartNums = cs.length;
        return breakResult;
    }

    public static BreakResult testToJava(String text, float measureWidth, float textPadding) {
        return testToJava(text.toCharArray(), measureWidth, textPadding);
    }
}
